/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.node;

import im.eventpackage.Event;
import im.eventpackage.EventGenerator;
import im.general.Config;
import im.messages.PublishMessage;
import im.messages.SubscriptionMessage;
import im.numgenerator.PseudoRandomGenerator;
import im.subscriptionpackage.ContentSubscription;
import im.subscriptionpackage.Subscription;
import im.subscriptionpackage.SubscriptionGenerator;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author bluejack
 */
public class PublishSubscribeBehaviour extends CyclicBehaviour {

    EventGenerator eventGenerator=null;
    List types=null;
    List topics=null;
    List objects=null;
    String hierarchy = null;
    MobileNodeAgent mobileNodeAgent;
    SubscriptionGenerator subscriptionGenerator;
        
    public PublishSubscribeBehaviour(MobileNodeAgent mobileNodeAgent) {

        this.mobileNodeAgent = mobileNodeAgent;
        this.eventGenerator = new EventGenerator(mobileNodeAgent.getPrg());
        this.subscriptionGenerator = new SubscriptionGenerator(mobileNodeAgent.getPrg());
        
        types=new ArrayList();
        topics=new ArrayList();
        objects=new ArrayList();
        
        /*String topic1="pizza";
        String topic2="kebab";
        String topic3="pub";
        String topic4="music";

        topics.add(topic1);
        topics.add(topic2);
        topics.add(topic3);
        topics.add(topic4);*/

        topics.add("pizza");

        

        /*String type1="Market";
        String type2="EnglishMarket";
        String type3="Disaster";
        String type4="Integer";

        types.add(type1);
        types.add(type2);
        types.add(type3);
        types.add(type4);*/

        types.add("Market");

        /*int value1=1;
        int value2=3;
        int value3=4;

        objects.add(value1);
        objects.add(value2);
        objects.add(value3);
*/
        objects.add((int)1);



        Iterator i = types.iterator();
        hierarchy = (String) i.next();
        while(i.hasNext())
            hierarchy.concat(" "+i.next());
        
    }
    @Override
    public void action() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(PublishSubscribeBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }

        
        mobileNodeAgent.sendMessage(generatePublishMessage());
        mobileNodeAgent.sendMessage(generateSubscriptionMessage());
    }

    public ACLMessage generateSubscriptionMessage() {
        Subscription s = subscriptionGenerator.generateSubscription(types, topics, objects, hierarchy);//""+PseudoRandomGenerator.getUniform()*100000, types, topics, objects);
            ACLMessage msg = new ACLMessage(ACLMessage.REQUEST_WHENEVER);

            SubscriptionMessage smsg = new SubscriptionMessage(s);
        try {
            msg.setContentObject((Serializable) smsg);
            //else
        } catch (IOException ex) {
            Logger.getLogger(PublishSubscribeBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }
            msg.setEncoding(Config.SUBSCRIBE_NAME);
            msg.setPerformative(ACLMessage.SUBSCRIBE);
            if(s instanceof ContentSubscription)
                mobileNodeAgent.addReceiver(mobileNodeAgent.getClusterHeadID(), msg);
            else
                mobileNodeAgent.addReceiver(Config.EVENT_SERVICE_NAME, msg);
            
            return msg;
    }

    public ACLMessage generatePublishMessage() {

        //if(PseudoRandomGenerator.getUniform()>0.5)
            Event e = eventGenerator.generateEvent(""+mobileNodeAgent.getPrg().getUniform()*100000, types, topics, objects);
            ACLMessage msg = new ACLMessage(ACLMessage.REQUEST_WHENEVER);

            //msg.setEncoding(Config.PUBLISH_NAME);
            //msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.PUBLISH_NAME);
            //msg.setPerformative(ACLMessage.INFORM);

            PublishMessage pmsg = new PublishMessage(e);
        try {
            msg.setContentObject((Serializable) pmsg);
            //else
        } catch (IOException ex) {
            Logger.getLogger(PublishSubscribeBehaviour.class.getName()).log(Level.SEVERE, null, ex);
        }
            mobileNodeAgent.addReceiver(Config.EVENT_SERVICE_NAME, msg);
           // msg.setPerformative(ACLMessage.REQUEST_WHENEVER);
           msg.setEncoding(Config.PUBLISH_NAME);
            return msg;
    }

}
